package com.hospital.service.sample.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.service.sample.model.param.*;
import com.hospital.service.target.model.param.TarProteinNucleicAcidExportParam;
import common.core.util.FileUtil;
import common.core.util.SystemLog;
import enhance.plus.logic.LogicBaseController;
import com.hospital.service.sample.entity.SamInfoDetails;
import com.hospital.service.sample.entity.help.SamInfoDetailsHelp;
import com.hospital.service.sample.service.SamInfoDetailsService;
import com.hospital.service.sample.model.vo.SamInfoDetailsVo;
import common.core.util.CommonUtil;
import common.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import enhance.plus.logic.LogicBaseController;
import enhance.plus.util.Page;
import enhance.plus.util.PageHelper;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;


/**
 * 实验样本详细表 控制器
 *
 * @author lfb
 * @created 2020-10-10 14:27:13
 */
@Slf4j
@Validated
@RestController
@Api(tags = "实验样本详细表")
@RequestMapping("/sample/samInfoDetails")
public class SamInfoDetailsController extends LogicBaseController<SamInfoDetailsService, SamInfoDetails> {

    @Autowired
    private SamInfoDetailsService samInfoDetailsService;

    @ApiOperation(value = "样本的特定组织和细胞样本编号生成")
    @RequestMapping(value = "/getSamplesDetailsNumber" , method = RequestMethod.GET)
    public Result getSamplesDetailsNumber(@Valid  SamInfoBaseParam param){
        log.debug("开始生成样本检索编号-->{}",param);
        return Result.success(this.samInfoDetailsService.getSamplesDetailsNumber());
    }


    @ApiOperation(value = "保存实验样本详细表的数据")
    @SystemLog(value = "保存实验样本详细", logo = "organizationNumber")
    @RequestMapping(value = "/save" , method = RequestMethod.PUT)
    public Result save(@Valid @RequestBody SamInfoDetailsParam param){
        log.debug("开始保存实验样本详细表-->{}",param);
        this.samInfoDetailsService.add(CommonUtil.copyBean(param,SamInfoDetails.class));
        return Result.success();
    }

    @ApiOperation(value = "更新实验样本详细表的数据")
    @SystemLog(value = "更新实验样本详细", logo = "organizationNumber")
    @RequestMapping(value = "/update" , method = RequestMethod.PUT)
    public Result update(@Valid @RequestBody SamInfoDetailsParam param){
        log.debug("开始跟新实验样本详细表-->{}",param);
        return Result.success(this.samInfoDetailsService.updateById(CommonUtil.copyBean(param,SamInfoDetails.class)));
    }

    @ApiOperation(value = "删除实验样本详细表的一条数据")
    @RequestMapping(value = "/delete" , method = RequestMethod.DELETE)
    @ApiImplicitParams(
        @ApiImplicitParam(name= "id" ,value = "数据ID", required = true , example = "-1" )
    )
    public Result delete(@Valid @RequestParam @NotBlank(message = "删除数据ID不能为空") String id){
        log.debug("开始删除实验样本详细表的数据-->{}",id);
        return Result.success(this.samInfoDetailsService.removeById(id));
    }

    @ApiOperation(value = "根据条件查询一条实验样本详细表的数据")
    @RequestMapping(value = "/queryForOneRow" , method = RequestMethod.POST)
    public Result<SamInfoDetailsVo> queryForOneRow(@Valid @RequestBody SamInfoDetailsQueryForOneRowParam param){
        SamInfoDetails row = this.samInfoDetailsService.getOne(param.convertParamToQueryWapper());
        return Result.success( CommonUtil.copyBean(row, SamInfoDetailsVo.class));
    }

    @ApiOperation(value = "根据条件查询实验样本详细表的数据")
    @RequestMapping(value = "/queryList" , method = RequestMethod.POST)
    public Result<List<SamInfoDetailsVo>> queryList(@Valid @RequestBody SamInfoDetailsQueryPageParam param){
        QueryWrapper<SamInfoDetails> query = SamInfoDetailsHelp.createQuery();
        query.like(SamInfoDetailsHelp.samplesNumber,param.getSamplesNumber());
        return Result.success(this.samInfoDetailsService.list(query));
    }

    @ApiOperation(value = "上传接口")
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file)
            throws IllegalStateException, IOException {
        return FileUtil.upload(file);
    }

    /**
     * 多文件上传
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "文件上传",notes = "文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files",value = "多个文件，",paramType = "formData",allowMultiple=true,required = true,dataType = "file"),
            @ApiImplicitParam(name = "uId",value = "上传用户ID，",paramType = "query",required = true,dataType = "Long")
    })
    @PostMapping(value = "/uplode/files", headers = "content-type=multipart/form-data")
    public void uplodeFiles(@RequestParam(value = "files") MultipartFile[] files,Long uId) throws IOException {
        int maxCount = files.length;
        for (int i = 0; i < maxCount; i ++) {
            FileUtil.upload(files[i]);
        }
    }

    @ApiOperation(value = "下载接口")
    @ApiImplicitParam(name = "downloadFilePath", value = "文件地址", paramType = "query", required = true)
    @GetMapping("/download")
    public void download(HttpServletResponse response, @RequestParam String downloadFilePath) {
        FileUtil.download(response, downloadFilePath);
    }

    @ApiOperation(value = "项目导出")
    @PostMapping(value = "/projectExport")
    public Result projectExport(@Valid @RequestBody SamInfoExportParam param
            , HttpServletResponse response) {
        this.samInfoDetailsService.projectExport(param, response);
        return Result.success();
    }
}
